/*!
 * @文件名 drv_adc.c
 *
 * @功能描述
 *  1.ADC模块底层驱动函数，一共有三路ADC采集
 */
#include "drv_adc.h"

uint16_t adcRawValue;//ADC实际采样返回值
uint16_t adcMax;	 //ADC采集分辨率

/*FUNCTION**********************************************************************
 *
 * Function Name : bsp_drv_adc_init
 * Description   : 初始化ADC模块，根据配置选择采用分辨率
 *
 *END**************************************************************************/
void bsp_drv_adc_init(void)
{
	if (adc_config_1_ConvConfig0.resolution == ADC_RESOLUTION_8BIT)
		adcMax = (uint16_t) (1 << 8);
	else if (adc_config_1_ConvConfig0.resolution == ADC_RESOLUTION_10BIT)
		adcMax = (uint16_t) (1 << 10);
	else
		adcMax = (uint16_t) (1 << 12);
	ADC_DRV_ConfigConverter(ADC_INSTANCE, &adc_config_1_ConvConfig0);
	ADC_DRV_AutoCalibration(ADC_INSTANCE);
}

/*FUNCTION**********************************************************************
 *
 * Function Name : bsp_drv_adc_read
 * Description   : 三路不同的ADC采样函数。第一路是板载MCU热采集，第二路是电池电压采集，第三路是背光温度采集
 *
 *END**************************************************************************/
uint16_t bsp_drv_adc_read(uint8_t channal)
{
	/* Configure ADC channel and software trigger a conversion */
	switch(channal)
	{
	case ADC_CHANNAL1:
		ADC_DRV_ConfigChan(ADC_INSTANCE, 0U, &adc_config_1_ChnConfig0);
		break;
	case ADC_CHANNAL2:
		ADC_DRV_ConfigChan(ADC_INSTANCE, 0U, &adc_config_1_ChnConfig1);
		break;
	case ADC_CHANNAL3:
		ADC_DRV_ConfigChan(ADC_INSTANCE, 0U, &adc_config_1_ChnConfig2);
		break;
	}
	/* Wait for the conversion to be done */
	ADC_DRV_WaitConvDone(ADC_INSTANCE);
	/* Store the channel result into a local variable */
	ADC_DRV_GetChanResult(ADC_INSTANCE, 0U, &adcRawValue);
	/* Process the result to get the value in volts */
	return adcRawValue;
}

